package com.gs.SynAntiWandering.activity.record;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.gs.SynAntiWandering.R;
import com.gs.SynAntiWandering.activity.base.BaseActivity;
import com.gs.SynAntiWandering.apiresult.ApiResultData;
import com.gs.SynAntiWandering.constant.AllConstants;
import com.gs.SynAntiWandering.control.LabelDateView;
import com.gs.SynAntiWandering.db.dto.ShortDto;
import com.gs.SynAntiWandering.db.query.RecordQuery;
import com.gs.SynAntiWandering.dialog.ShortDialog;
import com.gs.SynAntiWandering.presenter.FilterPresenter;
import com.gs.SynAntiWandering.util.ToastUtil;
import com.gs.SynAntiWandering.util.WindowUtil;
import com.gs.SynAntiWandering.view.IFilterView;

import java.util.ArrayList;
import java.util.List;

public class RecordFilterActivity extends BaseActivity<IFilterView, FilterPresenter> implements IFilterView {

    private LabelDateView lbDate, lbTime;
    private EditText etMemberName;
    private TextView tvType, tvFloor, tvDevice, tvIoType;
    private RecordQuery query;
    private int title;
    private String selectId;
    private List<ShortDto> listState;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_filter;
    }

    @Override
    protected FilterPresenter createPresenter() {
        return new FilterPresenter();
    }

    @Override
    public void per() {
        lbDate.setNull();
        lbTime.setNull();
        etMemberName.setText("");
        tvType.setText("");
        tvFloor.setText("");
        tvDevice.setText("");
        tvIoType.setText("");
        query.filter();
    }

    @Override
    protected void initView() {
        WindowUtil.setBarBgColor(this, R.color.green);
        OnClickListener onClickListener = new OnClickListener();
        lbDate = fd(R.id.lbDate);
        lbTime = fd(R.id.lbTime);
        etMemberName = fd(R.id.et_member_name);
        tvType = fd(R.id.tv_type);
        tvType.setOnClickListener(onClickListener);
        tvFloor = fd(R.id.tv_floor);
        tvFloor.setOnClickListener(onClickListener);
        tvDevice = fd(R.id.tv_device);
        tvDevice.setOnClickListener(onClickListener);
        tvIoType = fd(R.id.tv_io_type);
        tvIoType.setOnClickListener(onClickListener);
        fd(R.id.btn_search).setOnClickListener(onClickListener);
        query = (RecordQuery) getIntent().getSerializableExtra(AllConstants.WHAT);
        if (query != null) {
            if (query.getbAt() != null) {
                lbDate.setBat(query.getbAt());
                lbDate.setEat(query.geteAt());
            }
            if (query.getbTt() != null) {
                lbTime.setBat(query.getbTt());
                lbTime.setEat(query.geteTt());
            }
            etMemberName.setText(query.getName() == null ? "" : query.getName());
            tvType.setText(query.getMemberType() == null ? "" : query.getMemberType());
            tvFloor.setText(query.getFloor() == null ? "" : query.getFloor());
            tvDevice.setText(query.getDevice() == null ? "" : query.getDevice());
            tvIoType.setText(query.getIoTypeName() == null ? "" : query.getIoTypeName());
        }
    }

    @Override
    public void onGetComboSuccess(ApiResultData<List<ShortDto>> apiResult) {
        if (apiResult.getStatus() != 2000) {
            ToastUtil.showToast(apiResult.getMessage());
            return;
        }
        new ShortDialog(this, apiResult.getData(), title, selectId,
                bean -> {
                    if (title == R.string.p_sel_member_type) {
                        tvType.setText(bean.getName());
                        query.setMemberType(bean.getName());
                        query.setMemberTypeId(bean.getId());
                    } else if (title == R.string.p_sel_floor) {
                        tvFloor.setText(bean.getName());
                        query.setFloor(bean.getName());
                        query.setFloorId(bean.getId());
                    } else {
                        tvDevice.setText(bean.getName());
                        query.setDevice(bean.getName());
                        query.setDeviceId(bean.getId());
                    }
                }).show();
    }

    @Override
    public void onGetComboFails(Exception msg) {
        ToastUtil.showToast( R.string.network_error);
    }

    class OnClickListener implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            int id = v.getId();
            if (id == R.id.tv_type) {
                title = R.string.p_sel_member_type;
                selectId = query.getMemberTypeId();
                mPresenter.combo("memberTypes");
            } else if (id == R.id.tv_floor) {
                title = R.string.p_sel_floor;
                selectId = query.getFloorId();
                mPresenter.combo("floors");
            } else if (id == R.id.tv_device) {
                title = R.string.p_sel_device;
                selectId = query.getDeviceId();
                mPresenter.combo("devices");
            } else if (id == R.id.tv_io_type) {
                if (listState == null) {
                    listState = new ArrayList<>();
                    ShortDto bean3 = new ShortDto("1", getString(R.string.type_i));
                    ShortDto bean1 = new ShortDto("2", getString(R.string.type_o));
                    ShortDto bean2 = new ShortDto("3", getString(R.string.i_o_type));
                    listState.add(bean3);
                    listState.add(bean1);
                    listState.add(bean2);
                }
                new ShortDialog(RecordFilterActivity.this, listState, R.string.p_sel_io_type, query.getIoType() + "",
                        bean -> {
                            query.setIoType(Integer.valueOf(bean.getId()));
                            query.setIoTypeName(bean.getName());
                            tvIoType.setText(bean.getName());
                        }).show();
            } else if (id == R.id.btn_search) {
                if (!TextUtils.isEmpty(lbDate.getBat()) && TextUtils.isEmpty(lbDate.getEat())) {
                    ToastUtil.showToast(R.string.p_sel_end_date);
                    return;
                }
                if (TextUtils.isEmpty(lbDate.getBat()) && !TextUtils.isEmpty(lbDate.getEat())) {
                    ToastUtil.showToast(R.string.p_sel_start_date);
                    return;
                }
                if (!TextUtils.isEmpty(lbTime.getBat()) && TextUtils.isEmpty(lbTime.getEat())) {
                    ToastUtil.showToast(R.string.p_sel_end_time);
                    return;
                }
                if (TextUtils.isEmpty(lbTime.getBat()) && !TextUtils.isEmpty(lbTime.getEat())) {
                    ToastUtil.showToast(R.string.p_sel_start_time);
                    return;
                }
                if (TextUtils.isEmpty(lbDate.getBat())) {
                    query.setbAt(null);
                    query.seteAt(null);
                } else {
                    query.setbAt(lbDate.getBat());
                    query.seteAt(lbDate.getEat());
                }
                if (TextUtils.isEmpty(lbTime.getBat())) {
                    query.setbTt(null);
                    query.seteTt(null);
                } else {
                    query.setbTt(lbTime.getBat());
                    query.seteTt(lbTime.getEat());
                }
                if (TextUtils.isEmpty(etMemberName.getText())) {
                    query.setName(null);
                } else {
                    query.setName(etMemberName.getText().toString());
                }
                Intent intent = new Intent();
                intent.putExtra(AllConstants.WHAT, query);
                setResult(1, intent);
                finish();
            }
        }
    }

}
